/*
 * Created by JFormDesigner on Mon Jun 24 15:04:59 CST 2024
 */

package scct.zhang.ui;

import scct.zhang.dao.UserDao;
import scct.zhang.model.User;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author 张
 */
public class FormLogin extends JFrame {
    public FormLogin() {
        initComponents();
    }

    private void btnlogin(ActionEvent e) {
//        System.out.println("按钮点击了");
//        获取文本框的输入用户名和密码
        String name = txtname.getText();
        String pass = txtpass.getText();
        System.out.println(name + "," + pass);
//        name.matches("正则表达式")返回true成功，false失败
        if (!name.matches("^\\w{3,10}$")){
//            swing的消息对话框
            JOptionPane.showMessageDialog(null,"你输入的用户名不合法，请重新检查在输入");
            return;
        }
        if (!pass.matches("^\\w{3,10}$")){
            JOptionPane.showMessageDialog(null,"你输入的密码不合法，请重新检查后输入");
            return;
        }
        UserDao userDao = new UserDao();
        User user = userDao.login(name,pass);
        if (user==null){
            JOptionPane.showMessageDialog(this,"用户名或密码错误，请检查后重新输入");
        }else{
            JOptionPane.showMessageDialog(this,"登录成功");

            FromMain formMain = new FromMain();
            formMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            formMain.setSize(800,600);
//            居中
            formMain.setLocationRelativeTo(null);
//            formMain.set
            formMain.setVisible(true);
//            隐藏当前窗体
            this.dispose();
//            进入主窗体

        }

    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - zhang
        label1 = new JLabel();
        label2 = new JLabel();
        textPane1 = new JTextPane();
        btnlogin = new JButton();
        button2 = new JButton();
        txtname = new JTextField();
        label3 = new JLabel();
        txtpass = new JPasswordField();

        //======== this ========
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u7528\u6237\u540d");
        contentPane.add(label1);
        label1.setBounds(new Rectangle(new Point(50, 85), label1.getPreferredSize()));

        //---- label2 ----
        label2.setText("\u5bc6\u7801");
        contentPane.add(label2);
        label2.setBounds(new Rectangle(new Point(50, 130), label2.getPreferredSize()));
        contentPane.add(textPane1);
        textPane1.setBounds(165, 110, textPane1.getPreferredSize().width, 3);

        //---- btnlogin ----
        btnlogin.setText("\u767b\u5f55");
        btnlogin.addActionListener(e -> btnlogin(e));
        contentPane.add(btnlogin);
        btnlogin.setBounds(new Rectangle(new Point(95, 175), btnlogin.getPreferredSize()));

        //---- button2 ----
        button2.setText("\u6ce8\u518c");
        contentPane.add(button2);
        button2.setBounds(new Rectangle(new Point(220, 175), button2.getPreferredSize()));

        //---- txtname ----
        txtname.setText("tom");
        contentPane.add(txtname);
        txtname.setBounds(105, 80, 190, txtname.getPreferredSize().height);

        //---- label3 ----
        label3.setText("\u767b\u5f55");
        contentPane.add(label3);
        label3.setBounds(new Rectangle(new Point(175, 40), label3.getPreferredSize()));
        contentPane.add(txtpass);
        txtpass.setBounds(105, 130, 185, txtpass.getPreferredSize().height);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(400, 300);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - zhang
    private JLabel label1;
    private JLabel label2;
    private JTextPane textPane1;
    private JButton btnlogin;
    private JButton button2;
    private JTextField txtname;
    private JLabel label3;
    private JPasswordField txtpass;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
